//
//  HEReportReadViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/11/25.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEReportReadViewController.h"
#import "HERportReadHeadCell.h"
#import "HEReportServerCell.h"
#import "HEReportReadFooterCell.h"

@interface HEReportReadViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) GDTableView *tableView;
@property (nonatomic, strong) GDFormModel *formModel;
@property (nonatomic, strong) NSMutableArray *sections;

@end

@implementation HEReportReadViewController

- (GDFormModel *)formModel {
    if (!_formModel) {
        _formModel = [[GDFormModel alloc] init];
        _formModel.proxyVC = self;
    }
    return _formModel;
}

 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"报告解读";
 
    [self initUI];
    [self initCell];
}
 
- (void)initUI {
    _tableView = [[GDTableView alloc] initWithFrame:CGRectMake(0, ZXNavBarHeight, KScreenWidth, KScreenHeight- ZXNavBarHeight) style:UITableViewStyleGrouped];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.scrollsToTop = YES;
    _tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.01)];
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView.backgroundColor = KWhiteColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

- (void)initCell {
    // 总段
    _sections = [NSMutableArray array];
    NSMutableArray *classArr = [NSMutableArray array];
  
    GDFiterCellModel *headModel = [GDFiterCellModel new];
    headModel.cellKlass = HERportReadHeadCell.class;
    
 
    GDFiterCellModel *midelModel = [GDFiterCellModel new];
    midelModel.cellKlass = HEReportServerCell.class;
    
 
    GDFiterCellModel *footerModel = [GDFiterCellModel new];
    footerModel.cellKlass = HEReportReadFooterCell.class;
 
 
    // 添加的时候注意顺序，暂未对顺序做处理
    classArr = @[headModel, midelModel, footerModel].mutableCopy;
    [self.tableView mc_registerCellByFiterModels:classArr];

    for (GDFiterCellModel *model in classArr) {
         NSMutableArray *secs = [NSMutableArray array];
         
        // 隐藏未达到条件的cell
         if (model.cellDisplayType == GDCellDisplayNone) {
             continue;
         }
         else if (model.cellType == GDFiterCellTypeDoctorOrderItem) {
           
         }
         else if (model.cellType == GDFiterCellTypeOrderPay) {
            
         }
         else {
            [secs addObject:model];
        }
         
        [_sections addObject: secs];
     }
    
    [_tableView reloadData];
}
  

#pragma mark -
 

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSMutableArray *sec = _sections[section];
    return sec.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray *sec = _sections[indexPath.section];
    GDFiterCellModel *fiterModel = sec[indexPath.row];
    
    UITableViewCell *cell = [_tableView mc_dequeueReusableCellWithClassType:fiterModel.cellKlass];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    fiterModel.fiterCellModelBlock((MCTableBaseCell *)cell, fiterModel, self.formModel, indexPath);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
   NSMutableArray *sec = _sections[indexPath.section];
    GDFiterCellModel *fiterModel = sec[indexPath.row];
 
    if (fiterModel.cellType == GDFiterCellTypeDoctorServer) {
        return 145;
    }
    return [tableView fd_heightForCellWithIdentifier: NSStringFromClass(fiterModel.cellKlass) cacheByIndexPath:indexPath configuration:^(MCTableBaseCell *cell) {
               [cell refreshContent:fiterModel formModel:self.formModel indexPath:indexPath];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSMutableArray *sec = _sections[indexPath.section];
     GDFiterCellModel *fiterModel = sec[indexPath.row];
 
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    NSMutableArray *sec = _sections[section];
    GDFiterCellModel *fiterModel = sec.lastObject;
//
//    if (fiterModel.cellType == GDFiterCellTypeOrderPay) {
//        return CGFLOAT_MIN;
//    }
    if (section == 2) {
        return 10;
    }
    return CGFLOAT_MIN;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *v = [[UIView alloc] init];
    v.backgroundColor = CLightGrayColor;
    return v;
}

 

- (void)changeTabbarView {
  
}

 

@end
